package de.alt.rest.service;

import java.net.MalformedURLException;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import com.google.appengine.api.datastore.Entity;
import com.google.gson.Gson;

import de.alt.rest.constants.DBConstants;
import de.alt.rest.constants.NMAPriorityEnum;
import de.alt.rest.exception.NMAException;
import de.alt.rest.exception.NotLoggedInException;
import de.alt.rest.handler.CronHandler;
import de.alt.rest.handler.IpHandler;
import de.alt.rest.handler.SwitchHandler;
import de.alt.rest.util.GeneralSuperUtil;
import de.alt.rest.util.NotifyMyAndroid;

/**
 * The Class RestService.
 */
@Path("rest")
public class RestService {

	/** The Constant log. */
	private static final Logger log = Logger.getLogger(RestService.class.getName());

	/** The util. */
	private GeneralSuperUtil util = new GeneralSuperUtil();

	/**
	 * Do cron.
	 *
	 * @return the response
	 */
	@GET
	@Path("/cron")
	@Produces(MediaType.TEXT_PLAIN)
	public Response doCron() {
		return Response.ok(new CronHandler().doCron()).build();
	}
	
	/**
	 * Gets the ip.
	 *
	 * @param id the id
	 * @param plain the plain
	 * @return the ip
	 */
	@GET
	@Path("/ip/get")
	@Produces(MediaType.APPLICATION_JSON)
	public Response getIp(@QueryParam("id") String id, @QueryParam("plain") Boolean plain) {

		Entity e = util.getById(DBConstants.TABLE_ADDRESS, id);

		if (e == null)
			return Response.status(Status.NOT_ACCEPTABLE).build();
		
		if (plain) {
			return Response.ok(e.getProperty(DBConstants.TABLE_ADDRESS_ADDR)).build();
		} else {
			return Response.ok(new Gson().toJson(e)).build();
		}
	}

	/**
	 * Sets the ip.
	 *
	 * @param id the id
	 * @param ip the ip
	 * @return the response
	 */
	@GET
	@Path("/ip/set")
	@Produces(MediaType.APPLICATION_JSON)
	public Response setIp(@QueryParam("id") String id, @QueryParam("ip") String ip) {
		try {
			Entity address = new IpHandler().setIp(id, ip);
			return Response.ok(new Gson().toJson(address)).build();
		} catch (MalformedURLException e) {
			log.warning(e.getMessage());
			return Response.ok(e).build();
		}
	}
	
	/**
	 * Status.
	 * 
	 * @return the response
	 */
	@GET
	@Path("/switch")
	@Produces(MediaType.APPLICATION_JSON)
	public Response getSwitchStatus() {
		return Response.ok(new Gson().toJson(new SwitchHandler().getSwitchStatus())).build();
	}
	
	/**
	 * Do response switch.
	 *
	 * @param id the id
	 * @param action the action
	 * @return the response
	 */
	@POST
	@Path("/switch")
	@Produces(MediaType.APPLICATION_JSON)
	public Response doSwitch(@FormParam("id") String id, @FormParam("action") String action) {
		try {
			util.isUserLoggedIn();
			return Response.ok(new Gson().toJson(new SwitchHandler().doSwitch(id, action))).build();
		} catch (NotLoggedInException e) {
			log.warning("Unauthorized user trying to switch");
			return Response.ok(e.getMessage()).build();
		}

	}

	/**
	 * Gets the link.
	 * 
	 * @param req the req
	 * @return the link
	 */
	@GET
	@Path("/loginlink")
	@Produces(MediaType.APPLICATION_JSON)
	public Response getLoginLink(@Context HttpServletRequest req) {
		return Response.ok(new Gson().toJson(util.getLogInLink(req.getServerName()))).build();
	}

	/**
	 * Send mail.
	 *
	 * @param mailTo the mail to
	 * @param subject the subject
	 * @param text the text
	 * @return the response
	 */
	@GET
	@Path("/mail")
	@Produces(MediaType.TEXT_PLAIN)
	public Response sendMail(@QueryParam("mailTo") String mailTo, @QueryParam("subject") String subject, @QueryParam("text") String text) {
		try {
			util.isUserLoggedIn();
			util.sendMail(mailTo, subject, text);
		} catch (Exception e) {
			log.warning(e.getMessage());
			return Response.status(Status.NOT_ACCEPTABLE).build();
		}

		return Response.status(Status.OK).build();
	}
	
	/**
	 * Nma.
	 *
	 * @return the response
	 */
	@GET
	@Path("/nma")
	@Produces(MediaType.TEXT_PLAIN)
	public Response notifyMyAndroid() {
		
		try {
			util.isUserLoggedIn();
		} catch (NotLoggedInException e) {
			log.warning(e.getMessage());
			return Response.status(Status.NOT_ACCEPTABLE).build();
		}
		
		try {
			new NotifyMyAndroid().notify("AppEngine", "Test", "...", NMAPriorityEnum.NORMAL);
		} catch (NMAException e) {
			log.warning(e.getMessage());
			return Response.ok(e.getMessage()).build();
		}
		return Response.ok(Status.OK).build();
	}
	
}
